﻿////////////////////////////////////////////////////////////////////////////////////
//
// Module :  HybridClock.js
// Note: originally named "clock.js", developed by Microsoft for Silverlight 1.0
// 
// © 2007 Microsoft Corporation. All Rights Reserved.
//
// This file is licensed as part of the Silverlight 1.0 SDK, for details look 
// here: http://go.microsoft.com/fwlink/?LinkID=89144&clcid=0x409
//
////////////////////////////////////////////////////////////////////////////////////
// Modified by Dr. Alexander Bell, USA (www.alexanderbell.us) on 10/18/2008
// Added: Clock animated resizing, digital clock (Date/Time), layout enhancement
//
// Note : Optimized for Microsoft Silverlight 2.0 (2.0.31005.0)
// Support info at:  http://www.microsoft.com/silverlight/
////////////////////////////////////////////////////////////////////////////////////

// Revision history
/// <MOD AB 10/08/2008>
/// <MOD AB 12/05/2008>

var _ver = "2.0"; // corresponds to Microsoft Siverlight version 2

var sizeX = 350; // original width
var sizeY = 350; // original height

// set Clock width and corresponding animation properties
var scaleXfrom = 0;
var scaleXto =0.7;
var scaleXdur = "00:00:02";

// set Clock height and corresponding animation properties
var scaleYfrom = 0;
var scaleYto = 0.7;
var scaleYdur = "00:00:02";

/// </MOD AB> 


////////////////////////////////////////////////////////////////////////////////////
function CreateSilverlightClock() 
{
    var scene = new SlvClock();
    Silverlight.createObjectEx
    ({
        source: 'xaml/HybridClock.xaml',
        parentElement: document.getElementById("HybridClock"),
        id: 'HybridClock2',
        properties: {
            width: sizeX * scaleXto,
            height: sizeY * scaleYto,
            background: '#00000000',
            isWindowless: 'true',
            framerate: '30',
            version: _ver
        },
        events:
        { 
            onError: 'errorHandle',
            onLoad: Silverlight.createDelegate(scene, scene.handleLoad)
        },
        context: null
    });
}

////////////////////////////////////////////////////////////////////////////////////
Silverlight.createDelegate = function(instance, method)
{ return function() { return method.apply(instance, arguments); } }

////////////////////////////////////////////////////////////////////////////////////
function SlvClock() { }

SlvClock.prototype.handleLoad = function(control, userContext, rootElement) 
{
    var now = new Date();

    var hourAnimation = control.content.findName("hourAnimation");
    var minuteAnimation = control.content.findName("minuteAnimation");
    var secondAnimation = control.content.findName("secondAnimation");

    // <MOD AB 10/08/2008> 
    // scaleX and scaleY allow programmaticaly set clock size and scale animation
    var scaleX = control.content.findName("scaleXanimation");
    scaleX.from = scaleXfrom;
    scaleX.to = scaleXto;
    scaleX.Duration = scaleXdur;

    var scaleY = control.content.findName("scaleYanimation");
    scaleY.from = scaleYfrom;
    scaleY.to = scaleYto;
    scaleY.Duration = scaleYdur;

    // </MOD AB> 

    if (hourAnimation) {
        var hours = now.getHours();

        // We need to include minutes as well. Because each hour consists of 
        // 30 degrees, each additional minute adds half a degree to the angle
        // of the hour hand

        var angle = (hours / 12) * 360 + now.getMinutes() / 2;
        angle += 180;

        hourAnimation.from = angle.toString();
        hourAnimation.to = (angle + 360).toString();
    }

    if (minuteAnimation) {
        var minutes = now.getMinutes();
        var angle = (minutes / 60) * 360;
        angle += 180;

        minuteAnimation.from = angle.toString();
        minuteAnimation.to = (angle + 360).toString();
    }

    if (secondAnimation) {
        var seconds = now.getSeconds();
        var angle = (seconds / 60) * 360;
        angle += 180;

        secondAnimation.from = angle.toString();
        secondAnimation.to = (angle + 360).toString();
    }
}
////////////////////////////////////////////////////////////////////////////////////